package com.wksc.framework.impexp.template;

import java.util.ArrayList;
import java.util.Date;

import com.wksc.framework.impexp.bean.ExcelHeader;
import com.wksc.framework.impexp.bean.Header;
import com.wksc.framework.impexp.mode.Mode;
import jxl.demo.Write;

/**
 * <p> Title(文件名): ExpTemplate.java </p>
 * <p> Description(描述): TODO </p>
 * <p> Copyright(版权): Copyright (c) 2011 </p>
 * <p> Company(公司): 成都四方信息技术有限公司 </p>
 * <p> CreateTime(生成日期)：2011-10-25下午04:08:00 </p>
 * @author wanglei
 * @version impExp_C13
 */
public class ExpTemplate extends BaseTemplate {

	private static final long serialVersionUID = 1441788681690268238L;

	/*导出任务专有的属性*/
	/**导出任务时页面所显示的表头集合*/
	private Object[] showedHeader;
	
	/**查询数据的模板*/
	private QueryTemplate queryTemplate;
	
	/** 单元格合并属性 */
	private Object excelConflate;
	//导出文件的格式,支持excel,csv,txt,默认为单文件excel,多文件csv
	private Mode expFileMode = Mode.EXP_CLASSIC;
	
	
	private Integer dataRow = 1;
	
	/**
	 * 导出任务模板构造函数,此构造函数不包含showedHeader
	 * @param operator 操作员
	 * @param typeName 导出的模块名
	 * @param operatObj 操作对象
	 * @param fullHeader 该模块完整的表头
	 */
	public ExpTemplate(String operator, String typeName, int operatObj,
			Header[] fullHeader) {
		super(operator, typeName, operatObj, fullHeader);
		/*
		 * 此构造函数不提供shwoedHeader表示完整表头就等于显示表头 因此遍历完整表头构造一个显示表头
		 */
		Object[] tmp = new Object[fullHeader.length];
		for (int i = 0; i < tmp.length; i++) {
			if (fullHeader[i] instanceof ExcelHeader) {
				tmp[i] = fullHeader[i];
			} else {
				tmp[i] = fullHeader[i].getHeadLabel();
			}
		}
		this.showedHeader = tmp;
		init();
	}

	/**
	 * 导出任务模板构造函数
	 * @param operator 操作员
	 * @param typeName 导出的模块名
	 * @param operatObj 操作对象
	 * @param showedHeader 导出时页面显示的标头集合
	 * @param fullHeader 该模块完整的表头
	 */
	public ExpTemplate(String operator, String typeName, int operatObj,
			String[] showedHeader, Header[] fullHeader) {
		super(operator, typeName, operatObj, fullHeader);
		this.showedHeader = showedHeader;
		init();
	}

    /**
     * 当ExpTemplate初始化时调用此方法计算需要导出的列索引
     * 并清理和剔除不存在于fullHeader中的showedHeader
     */
    private void init() {
        //1. 初始化writeOrder
        if (null == writeOrder) {
            ArrayList<String> tmpShowedHeader = new ArrayList<String>(showedHeader.length);
            ArrayList<String> tmpFullHeader = new ArrayList<String>(fullHeader.length);
            ArrayList<Integer> index = new ArrayList<Integer>();

            //将fullHeader对象中表示表头的label取出来
            for (Header head : fullHeader) {
            	if (head instanceof ExcelHeader) {
            		//合并单元格后数据对应下面的列
            		if (((ExcelHeader) head).getColSpan() == 0) {
            			tmpFullHeader.add(head.getHeadLabel());
            		}
            	} else {
            		tmpFullHeader.add(head.getHeadLabel());
            	}
            }

            /*遍历页面显示的表头
            *  如果存在于完整表头中则将其索引值添加至index队列
            *  并将该showedHeader值添加至tmpshowedHeader中
            */
            for (int i = 0; i < showedHeader.length; i++) {
            	int e = -1;
            	if (showedHeader[i] instanceof ExcelHeader) {
            		e = tmpFullHeader.indexOf(((ExcelHeader) showedHeader[i]).getHeadLabel());
            	} else {
            		e = tmpFullHeader.indexOf(showedHeader[i]);
            	}
                if (-1 != e) {
                    index.add(e);
                    //将存在于fullHeader中的表头添加至tmpShowedHeader
                    if (!(showedHeader[i] instanceof ExcelHeader)) {
                    	tmpShowedHeader.add((String) showedHeader[i]);
                    }
                }
            }
            if (!(showedHeader[0] instanceof ExcelHeader)) {
            	showedHeader = tmpShowedHeader.toArray(new String[0]);
            }
            writeOrder = index.toArray(new Integer[0]);
        }
        
        //2.初始化日期
    	if(null == createTime){
    		setCreatTime(new Date());
    	}
       
    	//3.初始化格式化所需参数对象
//        formatParams = new BaseFormatParams(decipherRights,showContentRights);
        
        //4.初始化文件名
        StringBuffer tmpFileName = new StringBuffer();
        tmpFileName.append(operator).append("_").append(typeName).append("_").append(
                //CalendarTools.format(11, createTime)
                ""
        );
        setFileName(tmpFileName.toString());
    }

    /*=========setters & getters==============*/
	public Object[] getShowedHeader() {
		return showedHeader;
	}

	public QueryTemplate getQueryTemplate() {
		return queryTemplate;
	}

	public void setQueryTemplate(QueryTemplate queryTemplate) {
		this.queryTemplate = queryTemplate;
	}

	public Object getExcelConflate() {
		return excelConflate;
	}

	public void setExcelConflate(Object excelConflate) {
		this.excelConflate = excelConflate;
	}

	/**
	 * 获取导出文件的格式
	 * @return Mode枚举对象
	 */
	public Mode getExpFileMode() {
		return expFileMode;
	}

	/**
	 * 设置导出文件的格式<br>
	 * 支持excel,csv,txt,默认为单文件excel,多文件csv
	 * @param expFileMode 文件格式枚举对象
	 */
	public void setExpFileMode(Mode expFileMode) {
		this.expFileMode = expFileMode;
	}

	public Integer getDataRow() {
		return dataRow;
	}

	public void setDataRow(Integer dataRow) {
		this.dataRow = dataRow;
	}

	/*=========setters & getters==============*/
}
